﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration; 

namespace BLL
{
    public class AttachmentsBase
    {
         
        /// <summary>
        /// 匹配 complaintId 获取附件序列
        /// </summary>
        /// <param name="complaintId"></param>
        /// <returns></returns>
        public static Model.AttachmentsBase[] GetAttachments(string regardingObjectId)
        {
            return (new SQLServerDAL.AttachmentsBase()).GetAttachments(regardingObjectId);
        }

        /// <summary>
        /// 添加 / 修改附件
        /// </summary>
        /// <param name="_complaint"></param>
        /// <returns></returns>
        public static string AttachmentsAdmin(Model.AttachmentsBase[] items, string regardingObjectId)
        {
            return (new SQLServerDAL.AttachmentsBase()).AttachmentsAdmin(items, regardingObjectId);
        }

        #region 删除
        /// <summary>
        /// 匹配外键删除文件
        /// </summary>
        /// <param name="regardingObjectId"></param>
        /// <returns></returns>
        public static void RemoveFilesByRegardingObjectId(string regardingObjectId, string relativePath)
        {
            string result = "";
 
            Model.AttachmentsBase[] items = GetAttachments(regardingObjectId);
            try
            {
                string filePath = "";
                foreach (Model.AttachmentsBase _item in items)
                {
                    filePath = System.Web.HttpContext.Current.Server.MapPath("../../" + relativePath + _item.FilePath); 

                    if (System.IO.File.Exists(filePath))
                        System.IO.File.Delete(filePath);

                    filePath = System.Web.HttpContext.Current.Server.MapPath("../../" + relativePath + _item.FilePath.Trim().Replace(_item.ExtName, "_72x72px.png"));
                    if (System.IO.File.Exists(filePath))
                        System.IO.File.Delete(filePath);
                }
                result = "succeeded";
            }
            catch (Exception err) { result = err.Message; }

            if (result == "succeeded")
            {
                result = (new SQLServerDAL.AttachmentsBase()).DeleteAttachmentsByRegardingObjectId(regardingObjectId);
            }
        }
        /// <summary>
        /// 匹配外键删除文件记录
        /// </summary>
        /// <param name="regardingObjectId"></param>
        /// <returns></returns>
        public static string DeleteAttachmentsByRegardingObjectId(string regardingObjectId)
        {
            return (new SQLServerDAL.AttachmentsBase()).DeleteAttachmentsByRegardingObjectId(regardingObjectId);
        }
        #endregion

        #region Utils
        public static string GetIcoByExtName_Home(string extName)
        {
            string filetype = BuidFileTypeByExtName(extName);
            string imageName = "";
            switch (filetype)
            {
                case "1":
                    imageName = "<img alt=\"\" src=\"_imgs/attachments/16_doc.gif\">";
                    break;
                case "2":
                    imageName = "<img alt=\"\" src=\"_imgs/attachments/16_exe.gif\">";
                    break;
                case "3":
                    imageName = "<img alt=\"\" src=\"_imgs/attachments/16_htm.gif\">";
                    break;
                case "4":
                    imageName = "<img alt=\"\" src=\"_imgs/attachments/16_image.gif\">";
                    break;
                case "5":
                    imageName = "<img alt=\"\" src=\"_imgs/attachments/16_media.gif\">";
                    break;
                case "6":
                    imageName = "<img alt=\"\" src=\"_imgs/attachments/16_ppt.gif\">";
                    break;
                case "7":
                    imageName = "<img alt=\"\" src=\"_imgs/attachments/16_vsd.gif\">";
                    break;
                case "8":
                    imageName = "<img alt=\"\" src=\"_imgs/attachments/16_xls.gif\">";
                    break;
                case "9":
                    imageName = "<img alt=\"\" src=\"_imgs/attachments/16_pdf.png\">";
                    break;
                default:
                    imageName = "<img alt=\"\" src=\"_imgs/attachments/16_image.gif\">";
                    break;
            }
            return imageName;
        }
        public static string GetIcoByExtName(string extName)
        {
            string filetype = BuidFileTypeByExtName(extName);
            string imageName = "";
            switch (filetype)
            {
                case "1":
                    imageName = "<img alt=\"\" src=\"../_imgs/attachments/16_doc.gif\">";
                    break;
                case "2":
                    imageName = "<img alt=\"\" src=\"../_imgs/attachments/16_exe.gif\">";
                    break;
                case "3":
                    imageName = "<img alt=\"\" src=\"../_imgs/attachments/16_htm.gif\">";
                    break;
                case "4":
                    imageName = "<img alt=\"\" src=\"../_imgs/attachments/16_image.gif\">";
                    break;
                case "5":
                    imageName = "<img alt=\"\" src=\"../_imgs/attachments/16_media.gif\">";
                    break;
                case "6":
                    imageName = "<img alt=\"\" src=\"../_imgs/attachments/16_ppt.gif\">";
                    break;
                case "7":
                    imageName = "<img alt=\"\" src=\"../_imgs/attachments/16_vsd.gif\">";
                    break;
                case "8":
                    imageName = "<img alt=\"\" src=\"../_imgs/attachments/16_xls.gif\">";
                    break;
                case "9":
                    imageName = "<img alt=\"\" src=\"../_imgs/attachments/16_pdf.png\">";
                    break;
                default:
                    imageName = "<img alt=\"\" src=\"../_imgs/attachments/16_image.gif\">";
                    break;
            }
            return imageName;
        }

        public static string BuidFileTypeByExtName(string extName)
        {
            int start = extName.LastIndexOf(".");
            int end = extName.Length;
            extName = extName.Substring(start, end - start);

            string type = "0";
            string extNameList = ".xls|.xlsx|.doc|.docx|.ppt|.exe|.rar|.jpg|.pdf|.gif|.jpeg|.txt|.htm|.html|.png";

            extNameList = ".doc|.docx|.txt";
            if (extNameList.IndexOf(extName, 0) != -1)
            {
                type = "1";
            }
            extNameList = ".exe|.rar";
            if (extNameList.IndexOf(extName, 0) != -1)
            {
                type = "2";
            }
            extNameList = ".htm|.html";
            if (extNameList.IndexOf(extName, 0) != -1)
            {
                type = "3";
            }
            extNameList = ".jpg|.pdf|.gif|.png";
            if (extNameList.IndexOf(extName, 0) != -1)
            {
                type = "4";
            }
            extNameList = ".wav|.rm|.mp3";
            if (extNameList.IndexOf(extName, 0) != -1)
            {
                type = "5";
            }
            extNameList = ".ppt|.pptx";
            if (extNameList.IndexOf(extName, 0) != -1)
            {
                type = "6";
            }
            extNameList = ".vsd";
            if (extNameList.IndexOf(extName, 0) != -1)
            {
                type = "7";
            }
            extNameList = ".xls|.xlsx";
            if (extNameList.IndexOf(extName, 0) != -1)
            {
                type = "8";
            }
            extNameList = ".pdf";
            if (extNameList.IndexOf(extName, 0) != -1)
            {
                type = "9";
            }

            return type;
        } 
        #endregion
    }
}
